<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/
 * @license		GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

require_once (JPATH_COMPONENT_SITE.DS.'common_functions.php');



/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class TargetsModelTargets extends JModel
{
	/**
	 * Gets the greeting
	 * @return string The greeting to be displayed to the user
	 */
	var $_data;
	 
	function getTargets()
	{
		$db =& JFactory::getDBO();

		$query = 'SELECT id, target_name, p_id FROM #__trg_targets order by p_id';
		$db->setQuery( $query );
		$this->_data = $this->_getList( $query );
		
		$last = array('NULL' => 1);
		$prefix = array('NULL' => '');
		$numbers = array(1);
		$level = 0;
		for ($i = 0, $n = count($this->_data); $i < $n; $i++) {
			$pid = $this->_data[$i]->p_id;
			$id  = $this->_data[$i]->id;
			
			if ($pid == NULL) {
				$pid = 'NULL';
			}
			
			$this->_data[$i]->n = $prefix[$pid] . $last[$pid];
			$this->_data[$i]->n_array = explode('.', $this->_data[$i]->n);
			
			if ($last[$id] == NULL) {
				$last[$id] = 1;
				$prefix[$id] = $this->_data[$i]->n . '.';
			}
			else {
				$last[$id]++;
			}
			
			$last[$pid]++;
			//$this->_data[$i]->prefix = $prefix[$pid];
		}
		
		sort_targets($this->_data);
		
		return $this->_data;
	}
}
